iT邦幫忙

第 11 屆 iThome 鐵人賽

DAY 17
1
Software Development

PHP新手30天實戰金流系列 第 17

[Day17]平台串金流-- apple pay 未測試

  • 分享至 

  • xImage
  •  
PHP新手30天實戰金流, Laravel6

前言

ApplePaySDK


前置作業

測試工具

  • Mac
  • iphone(需設定 Apple Pay)

架設

  • 需使用 Https
  • 將資料夾放至 web root 之下

需要取得apple憑證檔案
*.crt.pem
*.key.pem

  • 注意憑證檔案請勿放在網頁公用目錄,以免遭竊取

設定測試參數

  • config.php
  • 其他檔案不異動

測試流程

完成前置作業
使用 USB 將手機連接電腦
開啟 checkout.php 頁面 (正常情況會出現付款按鈕)
電腦開啟 Safari 網頁開發模式 (有 console log,可以檢查在哪一個步驟失敗)
點擊付款按鈕執行付款流程


資料流程

checkout.php -> verify_vendor.php -> payment_gateway.php


程式介紹

checkout.php 產生applepay按鈕
verify_vendor.php 驗證廠商驗證
payment_gateway.php 傳送payment token 等交易資訊給綠界,進行信用卡授權
config.php 參數設定

ECPay.Payment.Applepay.php SDK V1.0.10603
applepay_button.css apple pay 按鈕CSS定義檔
applepay_button.js apple pay javascript前端程式
jquery-1.11.1.js jquery


  • checkout.php
<html>
<head>
<?php

/*
* step0 產生apple pay 按鈕
*/
include_once('config.php') ;
include_once('ECPay.Payment.Applepay.php') ;
$ecpay_applepay = new Ecpay_ApplePay ;
$ecpay_applepay->Applepay_Button['merchantIdentifier'] 	= APPLEPAY_MERCHANT_IDENTIFIER;
$ecpay_applepay->Applepay_Button['lable'] 		= APPLEPAY_LABLE;
$ecpay_applepay->Applepay_Button['step1_url'] 		= STEP1_URL;
$ecpay_applepay->Applepay_Button['step2_url'] 		= STEP2_URL;
$ecpay_applepay->Applepay_Button['debug_mode'] 		= DEBUG_MODE;
$ecpay_applepay->Applepay_Button['order_id'] 		= date('YmdHis');		// 廠商訂單編號
$ecpay_applepay->Applepay_Button['amount'] 		= '5'; 				// 訂單總金額
$ecpay_applepay->applepay_button();

?>

</head>
<body>

<div id="divPay">
<button id="btnApplePay" class="apple-pay-button apple-pay-button-white" lang="tw" style="-webkit-appearance: -apple-pay-button; -apple-pay-button-type: buy; width: 400px; height: 64px;"></button>
</div>

</body>

</html>
  • verify_vendor.php
<?php

/*
* step1 廠商驗證
*/

$validation_url = isset( $_POST['validationURL'] ) ? $_POST['validationURL'] : "https://apple-pay-gateway-cert.apple.com/paymentservices/startSession";

include_once('config.php') ;
include_once('ECPay.Payment.Applepay.php') ;
$ecpay_applepay = new Ecpay_ApplePay ;

$ecpay_applepay->ServiceURL 				= $validation_url ;

$ecpay_applepay->Verify_Vendor['displayName'] 		= APPLEPAY_DISPLAY_NAME;
$ecpay_applepay->Verify_Vendor['crt_path'] 		= APPLEPAY_CRT_PATH; 	// 憑證檔案絕對路徑
$ecpay_applepay->Verify_Vendor['key_path'] 		= APPLEPAY_KEY_PATH; 	// 憑證檔案絕對路徑
$ecpay_applepay->Verify_Vendor['key_password'] 		= APPLE_PASSWORD;

$aMsg_Return = $ecpay_applepay->Verify_Vendor();
echo json_encode($aMsg_Return);
?>
  • payment_gateway.php
<?php

/*
* step2 送出交易資訊到綠界
*/

$sPayment	= isset($_POST['payment'])	? $_POST['payment']	: '123456789abcdefgABCDEFG!@#$%^&*()' ;
$nOrder_Id	= isset($_POST['order_id'])	? $_POST['order_id']	: 0 ;

// 依照訂單編號$nOrder_Id找出訂單資訊
if(true)
{
	$TotalAmount 	= 5 ; 			// 訂單金額
	$ItemName 	= '手機20元X2#隨身碟60元X1' ; 	// 商品名稱
}

include_once('ECPay.Payment.Applepay.php') ;
$ecpay_applepay = new Ecpay_ApplePay ;

// 蒐集參數
$ecpay_applepay->ServiceURL 			= 'https://payment-stage.ecpay.com.tw/ApplePay/CreateServerOrder/V2' ;
$ecpay_applepay->MerchantID 			= '2000132' ;
$ecpay_applepay->HashKey 			= '5294y06JbISpM5x9' ;
$ecpay_applepay->HashIV 			= 'v77hoKGq4kWxNNIS' ;

$ecpay_applepay->Send['MerchantTradeNo'] 	= $nOrder_Id;
$ecpay_applepay->Send['MerchantTradeDate'] 	= date('Y/m/d H:i:s');
$ecpay_applepay->Send['CurrencyCode'] 		= 'TWD' ;
$ecpay_applepay->Send['TradeDesc'] 		= 'ecpay商城購物' ;
$ecpay_applepay->Send['PlatformID'] 		= '' ;
$ecpay_applepay->Send['TradeType'] 		= 2;
$ecpay_applepay->Send['PaymentToken'] 		= $sPayment;

$ecpay_applepay->Send['TotalAmount'] 		= $TotalAmount;
$ecpay_applepay->Send['ItemName'] 		= $ItemName;

//ServerPost 回傳是json格式
$aMsg_Return = $ecpay_applepay->Check_Out();

// 執行訂單相關程序
if(true)
{
	// 廠商自行撰寫
}

echo json_encode($aMsg_Return);

?>

上一篇
[Day16]平台串金流--退款 失敗 (ECpay 沒有退款的測試環境)
下一篇
[Day18]平台串金流--PAYPAL 簡介
系列文
PHP新手30天實戰金流34
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言